#!/usr/bin/env expect
############################################################################
# Purpose: Test of Slurm functionality
#          Validate scontrol show commands for jobs and steps.
#
# Output:  "TEST: #.#" followed by "SUCCESS" if test was successful, OR
#          "FAILURE: ..." otherwise with an explanation of the failure, OR
#          anything else indicates a failure mode that must be investigated.
############################################################################
# Copyright (C) 2002-2007 The Regents of the University of California.
# Copyright (C) 2008-2009 Lawrence Livermore National Security.
# Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
# Written by Morris Jette <jette1@llnl.gov>
# CODE-OCEC-09-009. All rights reserved.
#
# This file is part of Slurm, a resource management program.
# For details, see <https://slurm.schedmd.com/>.
# Please also read the included file: DISCLAIMER.
#
# Slurm is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free
# Software Foundation; either version 2 of the License, or (at your option)
# any later version.
#
# Slurm is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along
# with Slurm; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA.
############################################################################
source ./globals

set test_id     "2.8"
set exit_code   0
set job_id1     0
set job_id2     0

print_header $test_id

set select_type [test_select_type]
set def_part_name [default_partition]
set nb_nodes [get_node_cnt_in_part $def_part_name]
if {![string compare $select_type "linear"]} {
	if {$nb_nodes < 2} {
		send_user "\nWARNING: This test is incompatible with select/linear and only one node\n"
		exit $exit_code
	}
} elseif {[default_part_exclusive]} {
	if {$nb_nodes < 2} {
		send_user "\nWARNING: This test is incompatible with exclusive node allocations and only one node\n"
		exit $exit_code
	}
}

proc end_it {code} {
	global job_id1 job_id2 bin_rm test_id

	if {$job_id1} {
		cancel_job $job_id1
	}
	if {$job_id2} {
		cancel_job $job_id2
	}
	exec $bin_rm -f slurm-$job_id1.sh $test_id.sh
	if {$code == 0} {
		send_user "\nSUCCESS\n"
	}
	exit $code
}

if {[test_launch_poe]} {
	set step_id 1
} else {
	set step_id 0
}

#
# Submit a couple jobs so we have something to work with
#
set sbatch_pid [spawn $sbatch --output=/dev/null --error=/dev/null -t5 --wrap "$srun $bin_sleep 60"]
expect {
	-re "Submitted batch job ($number)" {
		set job_id1 $expect_out(1,string)
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: srun not responding\n"
		slow_kill $sbatch_pid
		exit 1
	}
	eof {
		wait
	}
}
if {$job_id1 == 0} {
	send_user "\nFAILURE: srun submit failed\n"
	exit 1
}

set sbatch_pid [spawn $sbatch --output=/dev/null --error=/dev/null -t5 --wrap "$srun $bin_sleep 60"]
expect {
	-re "Submitted batch job ($number)" {
		set job_id2 $expect_out(1,string)
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: srun not responding\n"
		cancel_job $job_id1
		slow_kill $sbatch_pid
		exit 1
	}
	eof {
		wait
	}
}
if {$job_id2 == 0} {
	send_user "\nFAILURE: srun submit failed\n"
	cancel_job $job_id1
	exit 1
}

if {[wait_for_job $job_id1 "RUNNING"] != 0} {
        send_user "\nFAILURE: waiting for job $job_id1 to start\n"
        cancel_job $job_id1
        cancel_job $job_id2
        exit 1
}
if {[wait_for_job $job_id2 "RUNNING"] != 0} {
        send_user "\nFAILURE: waiting for job $job_id2 to start\n"
        cancel_job $job_id1
        cancel_job $job_id2
        exit 1
}

#
# Look for these jobs with scontrol
#
# NOTE: Running "scontrol show job" and looking for these job IDs
# may not work due to a bug in awk not scanning large output buffers
#
# NOTE: Match "JobId=" only
#
send_user "\nGetting more info for job id 1 ($job_id1)\n"
set matches 0
spawn $scontrol show job $job_id1
expect {
	-re "JobId=$job_id1 " {
		incr matches
		exp_continue
	}
	-re "CPU_IDs=" {
		incr matches
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: scontrol not responding\n"
		end_it 1
	}
	eof {
		wait
	}
}
send_user "\nGetting more info for job id 2 ($job_id2)\n"
spawn $scontrol show job $job_id2
expect {
	-re "JobId=$job_id2 " {
		incr matches
		exp_continue
	}
	-re "CPU_IDs=" {
		incr matches
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: scontrol not responding\n"
		end_it 1
	}
	eof {
		wait
	}
}
if {$matches != 2} {
	send_user "\nFAILURE: scontrol failed to find matching job ($matches != 2)\n"
	end_it 1
}

#
# using the scontrol -d option to display more info about job
#
send_user "\nGetting more info for job id 1 ($job_id1) with -d option\n"
set matches 0
spawn $scontrol -d show job $job_id1
expect {
	-re "JobId=$job_id1 " {
		incr matches
		exp_continue
	}
	-re "CPU_IDs=" {
		incr matches
		exp_continue
	}
	timeout {
		send_user "\n FAILURE scontrol not responding\n"
		end_it 1
	}
	eof {
		wait
	}
}
send_user "\nGetting more info for job id 2 ($job_id2) with -d option\n"
spawn $scontrol -d show job $job_id2
expect {
	-re "JobId=$job_id2 " {
		incr matches
		exp_continue
	}
	-re "CPU_IDs=" {
		incr matches
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: scontrol not responding\n"
		end_it 1
	}
	eof {
		wait
	}

}
set match_target 4
if {$matches != $match_target} {
	send_user "\nFAILURE: scontrol failed to find matching job ($matches != $match_target)\n"
	end_it 1
}

#
# using the scontrol -dd option to display more info about job
#
send_user "\nGetting more info for job id 1 ($job_id1) with -dd option\n"
set matches 0
spawn $scontrol -dd show job $job_id1
expect {
	-re "JobId=$job_id1 " {
		incr matches
		exp_continue
	}
	-re "CPU_IDs=" {
		incr matches
		exp_continue
	}
	timeout {
		send_user "\n FAILURE scontrol not responding\n"
		end_it 1
	}
	eof {
		wait
	}
}
send_user "\nGetting more info for job id 2 ($job_id2) with -dd option\n"
spawn $scontrol -dd show job $job_id2
expect {
	-re "JobId=$job_id2 " {
		incr matches
		exp_continue
	}
	-re "CPU_IDs=" {
		incr matches
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: scontrol not responding\n"
		end_it 1
	}
	eof {
		wait
	}

}

set match_target 4
if {$matches != $match_target} {
	send_user "\nFAILURE: scontrol failed to find matching job ($matches != $match_target)\n"
	end_it 1
}

#
# Look at a specific job with scontrol
#
set matches 0
spawn $scontrol show job $job_id1
expect {
	-re "JobId=$job_id1 " {
		incr matches
		exp_continue
	}
	-re "JobId=" {
		send_user "\nFAILURE: scontrol filter by job id\n"
		end_it 1
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: scontrol not responding\n"
		end_it 1
	}
	eof {
		wait
	}
}
if {$matches != 1} {
	send_user "\nFAILURE: scontrol failed to find requested job\n"
	end_it 1
}

#
# Wait for step to start
#
sleep 10

#
# Look at all job steps with scontrol
#
set matches 0
spawn $scontrol show step
expect {
	-re "StepId=$job_id1.$step_id " {
		incr matches
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: scontrol not responding\n"
		end_it 1
	}
	eof {
		wait
	}
}
if {[test_alps] == 0 && $matches != 1} {
	send_user "\nFAILURE: scontrol failed to find all job steps\n"
	end_it 1
}

#
# Look at specific job step with scontrol
#
set matches 0
spawn $scontrol show step $job_id1.$step_id
expect {
	-re "StepId=$job_id1.$step_id " {
		incr matches
		exp_continue
	}
	-re "StepId=" {
		send_user "\nFAILURE: scontrol filter by step id\n"
		end_it 1
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: scontrol not responding\n"
		end_it 1
	}
	eof {
		wait
	}
}
if {[test_alps] == 0 && $matches != 1} {
	send_user "\nFAILURE: scontrol failed to specific job step\n"
	end_it 1
}

#
# Look at specific _invalid_ job step with scontrol
# (At least it should be invalid)
#
set matches 0
set job_id3 [expr $job_id1 + 10000]
spawn $scontrol show step $job_id3.$step_id
expect {
	-re "StepId=$job_id3.$step_id " {
		send_user "\nFAILURE: scontrol found unexpected job step\n"
		end_it 1
		exp_continue
	}
	-re "Invalid job id" {
		send_user "\nNo worries, error expected\n"
		incr matches
		exp_continue
	}
	-re "StepId=" {
		send_user "\nFAILURE: scontrol filter by step id\n"
		end_it 1
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: scontrol not responding\n"
		end_it 1
	}
	eof {
		wait
	}
}
if {[test_alps] == 0 && $matches != 1} {
	send_user "\nFAILURE: scontrol found unexpected job step\n"
	end_it 1
}


#
# write batch_script to file
#
set matches 0
spawn $scontrol write batch_script $job_id1
expect {
	-re "batch script for job $job_id1 written to slurm-$job_id1.sh" {
		incr matches
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: scontrol not responding\n"
		end_it 1
	}
	eof {
		wait
	}
}
if {$matches != 1} {
	send_user "\nFAILURE: scontrol failed to write batch script\n"
	end_it 1
}

set matches 0
spawn $scontrol write batch_script $job_id2 $test_id.sh
expect {
	-re "batch script for job $job_id2 written to $test_id.sh" {
		incr matches
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: scontrol not responding\n"
		end_it 1
	}
	eof {
		wait
	}
}
if {$matches != 1} {
	send_user "\nFAILURE: scontrol failed to write batch script\n"
	end_it 1
}

set matches 0
spawn ls slurm-$job_id1.sh
expect {
	-re "ls: cannot access" {
		end_it 1
	}
	-re "slurm-$job_id1.sh" {
		incr matches
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: scontrol not responding\n"
		end_it 1
	}
	eof {
		wait
	}
}
if {$matches != 1} {
	send_user "\nFAILURE: Could not find file\n"
	end_it 1
}

set matches 0
spawn ls $test_id.sh
expect {
	-re "ls: cannot access" {
		end_it 1
	}
	-re "$test_id.sh" {
		incr matches
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: scontrol not responding\n"
		end_it 1
	}
	eof {
		wait
	}
}
if {$matches != 1} {
	send_user "\nFAILURE: Could not find file\n"
	end_it 1
}

end_it 0
